<html>
	<head></head>
	<meta name="referrer" content="no-referrer" />
	<meta name="viewport"
		content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover" />
	<title>传入js字符串并执行</title>
	<body>
		<script>
			//检测plus是否准备好了
			function plusReady(callback) {
				if (window.plus) {
					callback();
				} else {
					document.addEventListener('plusready', callback);
				}
			};
			/**
			 * 执行函数
			 * @param { String } jsCode js代码字符串
			 * @param { String } id 唯一ID，用于返回
			 */
			function evalJS(jsCode, id) {
				plusReady(function() {
					setTimeout(function() {
						try {
							jsCode = JSON.parse(decodeURIComponent(jsCode))
							jsCode = 'return new Promise((resolve, reject) => {' + jsCode + '})'
							let jsFunction = new Function(jsCode);
							jsFunction().then(res => {
								plus.webview.postMessageToUniNView({
									result: res,
									errCode: 0,
									errMsg: 'ok',
									webWorker: true,
									webWorkerId: id
								}, '__uniapp__service')
							}).catch(err => {
								plus.webview.postMessageToUniNView({
									errCode: 2,
									errMsg: err,
									webWorker: true,
									webWorkerId: id
								}, '__uniapp__service')
							})
						} catch (e) {
							plus.webview.postMessageToUniNView({
								errCode: 1,
								errMsg: e.toString(),
								webWorker: true,
								webWorkerId: id
							}, '__uniapp__service')
						}
					}, 100)
				})
			}
		</script>
	</body>
</html>